/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.tom.social.analyzer.visualizer;

import edu.uci.ics.jung.algorithms.blockmodel.EquivalenceRelation;
import edu.uci.ics.jung.algorithms.cluster.ClusterSet;
import edu.uci.ics.jung.graph.Vertex;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import pl.tom.social.analyzer.common.CentralityType;
import pl.tom.social.analyzer.analysis.TermFreqCounter;
import pl.tom.social.dal.SocialDatabaseFacade;
import pl.tom.social.dal.entity.Profile;
import pl.tom.social.dal.entity.SiteEntry;
import pl.tom.social.dal.entity.SiteEntryComment;

/**
 *
 * @author Tom
 */
public class AnalysisVisualizer {

	private static final Logger logger = Logger.getLogger(AnalysisVisualizer.class);

	public static DefaultTableModel termFreqToJTable(DefaultTableModel model, String site, TermFreqCounter counter,
		int limit) {
		if (model == null) {
			model = newModel();
			model.addColumn("Term");
			model.addColumn("Site url");
			model.addColumn("Count");
		}
		Map<String, Integer> termResult = counter.get(limit);

		for (String term : termResult.keySet()) {
			model.addRow(new Object[]{term, site, termResult.get(term)});
		}

		return model;
	}

	public static DefaultTableModel profilesToJTable() {
		SocialDatabaseFacade db = new SocialDatabaseFacade();
		List<Profile> profs = db.getAllProfiles();
		DefaultTableModel model = newModel();
		model.addColumn("Name");
		model.addColumn("Query");
		for (Profile prof : profs) {
			model.addRow(new Object[]{prof.getName(), prof.getQuery()});
		}
		return model;
	}

	public static DefaultTableModel queryToJTable(Map<String, Float> scores) {
		DefaultTableModel model = newModel();
		model.addColumn("Url", scores.keySet().toArray());
		model.addColumn("Score", scores.values().toArray());
		return model;
	}

	public static DefaultTableModel commentsToJTable(List<SiteEntryComment> comments) {
		DefaultTableModel model = newModel();
		model.addColumn("Url");
		model.addColumn("Title");
		for (SiteEntryComment comment : comments) {
			model.addRow(new Object[]{comment.getUrl(), comment.getTitle()});
		}

		return model;
	}

	public static TableModel centralityToJTable(Map<String, Double> scores, CentralityType centralityType) {
		DefaultTableModel model = newModel();
		model.addColumn("Url", scores.keySet().toArray());
		model.addColumn("Score", scores.values().toArray());
		return model;
	}

	public static TableModel similarEntriesToJTable(Map<String, Float> result) {
		DefaultTableModel model = newModel();
		SocialDatabaseFacade db = new SocialDatabaseFacade();
		model.addColumn("Url");
		model.addColumn("Added");
		model.addColumn("Score");
		for (String url : result.keySet()) {
			SiteEntry entry = null;
			try {
				entry = db.getSiteEntry(url);
			} catch (Exception ex) {
				logger.log(Level.ERROR, ex);
			}
			if (entry != null) {
				model.addRow(new Object[]{url, entry.getAdded(), result.get(url)});
			} else {
				model.addRow(new Object[]{url, null, result.get(url)});
			}
		}
		return model;
	}

	public static TableModel clusterSetToJTable(ClusterSet result) {
		DefaultTableModel model = newModel();
		model.addColumn("Url");
		model.addColumn("Cluster");

		for (int i = 0; i < result.size(); i++) {
			Object[] cluster = result.getCluster(i).toArray();
			for (int j = 0; j < cluster.length; j++) {
				Vertex v = (Vertex) cluster[j];
				model.addRow(new Object[]{v.getUserDatum("url"), i});
			}
		}

		return model;
	}

	public static TableModel equivalenceRelationToJTable(EquivalenceRelation se) {
		DefaultTableModel model = newModel();
		model.addColumn("Url");
		model.addColumn("Group");
		int i = 0;
		for (Iterator iter = se.getAllEquivalences(); iter.hasNext(); i++) {
			Object obj = iter.next();
			if (obj instanceof Set) {
				Object[] vs = ((Set) obj).toArray();
				for (int j = 0; j < vs.length; j++) {
					if (vs[j] instanceof Vertex) {
						Vertex v = (Vertex) vs[j];
						model.addRow(new Object[]{v.getUserDatum("url"), i});
					}
				}
			}
		}

		return model;
	}

	public static TableModel setsToJTable(Set<Set<String>> sets) {
		DefaultTableModel model = newModel();
		model.addColumn("Url");
		model.addColumn("Cluster");
		int i = 0;
		for (Iterator<Set<String>> iter = sets.iterator(); iter.hasNext(); i++) {
			Set<String> single = iter.next();
			for (String str : single) {
				model.addRow(new Object[]{str, i});
			}
		}

		return model;
	}

	public static TableModel sumToJTable(Map<Double, Integer> map) {
		DefaultTableModel model = newModel();
		model.addColumn("Parameter");
		model.addColumn("Clusters");
		for (Iterator<Double> iter = map.keySet().iterator(); iter.hasNext();) {
			Double i = iter.next();
				model.addRow(new Object[]{i, map.get(i)});
		}

		return model;
	}

	private static DefaultTableModel newModel() {
		DefaultTableModel model = new DefaultTableModel() {

			@Override
			public Class<?> getColumnClass(int columnIndex) {
				Class returnValue;
				if ((columnIndex >= 0) && (columnIndex < getColumnCount())) {
					returnValue = getValueAt(0, columnIndex).getClass();
				} else {
					returnValue = Object.class;
				}
				return returnValue;
			}
		};

		return model;
	}
}
